<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\BuyWill\BuyWillDeleteAction;
use App\Admin\Actions\BuyWill\BuyWillTransStatusAction;
use App\Admin\Grid\NGrid;
use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopUserLogic;
use App\Library\Traits\AdminControllerTrait;
use App\Models\BuyWillModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Filter;

class BuyWillController extends AdminController
{
    protected $title = '随手买';

    use AdminControllerTrait;

    protected function grid(): NGrid
    {
        $grid = new NGrid(new BuyWillModel);
        $grid->model()->orderByDesc('created_at');
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->equal('id', 'ID')->integer();
                $filter->equal('status', __('buy_will.status'))->select(__('buy_will.statusArr'));
            });
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->equal('serial_number', __('l.serialNumber'));
                $filter->between('created_at', __('l.createdAt'))->datetime();
            });
        });
        $grid->column('id', 'ID')->sortable();
        $grid->column('serial_number', __('l.serialNumber'))->sortable();
        $grid->column('file_id', __('l.fileId'))->display(function ($field) {
            return fileIdToUrl($field);
        })->image(config('oss.url'), 60, 60);
        $grid->column('title', __('l.title'));
        $grid->column('description', __('l.description'))->display(function () {
            return view('buyWill.description', [
                'id' => $this->id,
                'description' => $this->description,
            ]);
        });
        $grid->column('status', __('buy_will.status'))->using(__('buy_will.statusArr'));
        $grid->column('sales', __('buy_will.sales'));
        $grid->column('original_price', __('buy_will.originalPrice'));
        $grid->column('price', __('buy_will.price'));
        $grid->column('stock', __('buy_will.stock'));
        $grid->column('limit', __('buy_will.limit'));
        $grid->column('created_at', __('l.createdAt'));
        $grid->disableCreateButton(false);
        $grid->actions(function (Actions $actions) {
            $actions->disableEdit(false);
            $actions->disableView();
            $actions->disableDelete();
            $actions->add(new BuyWillTransStatusAction($actions->row->status));
            $actions->add(new BuyWillDeleteAction);
        });
        $grid->disableActions(false);
        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new BuyWillModel);
        $form->hidden('file_id');
        $form->hidden('ossPrefix')->default(config('oss.url'));
        $form->ignore(['ossPrefix']);
        $form->hidden('serial_number');
        if ($form->isCreating()) {
            $form->hidden('user_id');
            $form->hidden('add_user_id');
        }
        $form->column(1 / 2, function (Form $form) {
            $form->display('file_id', __('l.fileId'))->with(function () use ($form) {
                return view('upload_image');
            })->required();
            $form->textarea('description', __('l.description'))->attribute('maxlength', 255)->rules('max:255');
        });
        $form->column(1 / 2, function (Form $form) {
            $form->text('title', __('l.title'))->attribute('maxlength', 30)->rules('max:30')->required();
            $form->select('status', __('l.status'))->options(__('buy_will.statusArr'))->required();
            $form->currency('original_price', __('buy_will.originalPrice'))->symbol('￥')->required();
            $form->currency('price', __('buy_will.price'))->symbol('￥')->required();
            $form->number('stock', __('buy_will.stock'))->default(0)->rules('min:0')->required();
            $form->number('limit', __('buy_will.limit'))->default(0)->rules('min:0')->required();
        });
        $form->saving(function (Form $form) {
            if ((float)$form->original_price < 0) return $this->backWith('原价不能小于0');
            else if ((float)$form->price < 0) return $this->backWith('价格不能小于0');
            else if ((int)$form->stock < 0) return $this->backWith('库存数不能小于0');
            else if ((int)$form->limit < 0) return $this->backWith('每人限购数不能小于0');
            if ($form->isCreating()) {
                $userId = Admin::user()->user_id;
                $mainId = ShopUserLogic::valueByWhere('userId', $userId, 'userMainId');
                $form->user_id = is_null($mainId) || $mainId === 0 ? $userId : $mainId;
                $form->add_user_id = $userId;
                $form->serial_number = serialNumber();
            }
            if (is_null($form->file_id)) $form->file_id = __('com.defaultImg');
            return null;
        });
        $form->saved(function () {
            OperateLogLogic::add(Admin::user()->user_id);
        });
        $form->setWidth(10);
        return $form;
    }
}
